package sharemem

import (
	"log"
)

const (
	POS_CD_TIME					= 1		//周期触发

	POS_GAME_BEGIN				= 2		//战斗开始后
	POS_DRAW_CARD				= 3		//队伍抽卡后
	POS_MY_DEAD					= 4		//我方角色阵亡后

	POS_ME_SKILL_2				= 11	//自已释放tag=2的技能卡后
	POS_ME_SKILL_23				= 12	//自已释放tag=2,3的技能卡后
	POS_ME_SKILL_3times			= 13	//自己释放三张卡技能后
	POS_ME_SKILL_3				= 14	//自已释放tag=3的技能卡后
	POS_ME_SKILL_KILL			= 15	//自己技能被打断后

	POS_ME_EFFECT_DMG_BEGIN		= 20	//自已伤害效果生效前
	POS_ME_EFFECT_DMG_DOUBLE	= 21	//自已伤害效果(发生暴击)生效后
	POS_ME_EFFECT_DMG_END		= 22	//自已伤害效果生效后
	POS_ME_EFFECT_BUFF_CLEAR	= 23	//自已驱散BUFF效果生效后
	POS_ME_EFFECT_TAG_1			= 24	//自已tag=1的效果生效后
	POS_MY_EFFECT_BUFF_ADD		= 25	//我方添加增益buff效果生效后
	POS_MY_EFFECT_DEBUFF_ADD	= 26	//我方添加减益buff效果生效后
	POS_ME_EFFECT_CURE			= 27	//自已治疗效果生效后
	POS_ME_EFFECT_TAG_2			= 28	//自已tag=2的效果生效后
	POS_ME_EFFECT_DMG_DEAD		= 29	//自己伤害效果击杀敌人后
	POS_ME_EFFECT_TAG_23		= 30	//自己tag=2/3的效果生效后
	POS_ME_EFFECT_TAG_4			= 31	//自己tag=4的效果生效后
	POS_ME_EFFECT_DEBUFF_ADD	= 32	//自己的减益buff效果生效后
	POS_ME_EFFECT_TAG5_DEAD		= 33	//自己被tag=5的效果击杀后
	POS_ME_EFFECT_TOUGH			= 34	//自己造成韧性伤害后

	POS_ME_ATTR_SPEED			= 41	//自已速度属性操作后
	POS_ME_ATTR_HP				= 42	//自己血量属性操作后
	POS_OTHER_ATTR_TOUGH_0		= 43	//敌方韧性属性操作后 (韧性值=0 且 韧性最大值!=0 时)
	POS_ME_ATTR_TOUGH_0			= 44	//自己韧性属性操作后 (韧性值=0 且 韧性最大值!=0 时)
	POS_ME_DMG					= 45	//自己受到伤害时

	POS_ME_BUFF_ADD				= 91	//自己被添加buff操作后
	POS_ME_BUFF_ADD_1			= 92	//自己被添加tag=1的buff后
)

func (trigger *Battle_unit)PassiveStart(target *Battle_unit,passive_type int8) {
	for _,v := range *trigger.GetPassiveMap(passive_type) {
		if v.begin_time > trigger.area.real_time {
			continue
		}
		log.Printf("@@@@@@@@@@@@@@ 触发被动 type = %d",passive_type)
		v.begin_time += v.cd_time
		trigger.BuildPassivePool(target,v)
		if passive_type == POS_CD_TIME {
			trigger.area.SetRealEventMap(v.begin_time)
		}
	}
}

func (trigger *Battle_unit)BuildPassivePool(target *Battle_unit,cfg *Cfg_skill_passive) {
	pool := &Skill_passive_pool {
		index:0,
	}
	for _,v := range cfg.skill_child {
		child := &Skill_child {
			Type:v.Type,
		}
		for _,v1 := range v.skill_effect {
			tmp_effect := v1.BuildSkillEffect(trigger,target)
			if tmp_effect == nil {
				continue
			}
			child.effect = append(child.effect,tmp_effect)
		}
		if len(child.effect) <= 0 {
			log.Printf("build skill passive effect nil.")
			continue
		}
		pool.child = append(pool.child,child)
	}
	if len(pool.child) <= 0 {
		log.Printf("build skill passive child nil.")
		return
	}
	//
	if !trigger.CheckPassivePool(pool) {
		cfg.pool = pool
	}
}

func (trigger *Battle_unit)CheckAllPassivePool() {
	for _,v := range trigger.run_passive {
		if v.pool == nil {
			continue
		}
		if trigger.CheckPassivePool(v.pool) {
			v.pool = nil
		}
	}
}

func (trigger *Battle_unit)CheckPassivePool(p *Skill_passive_pool) bool {
	if !p.child[p.index].CheckChild() {
		return false
	}
	p.index++
	if p.index < 0 || int(p.index) >= len(p.child) {
		//已执行完毕 需清理
		return true
	}
	p.child[p.index].SetTime()
	return trigger.CheckPassivePool(p)
}
